package com.sendpo.zjx.sendpo.activity;

import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.sendpo.zjx.sendpo.R;
import com.sendpo.zjx.sendpo.domain.UserNeedHelpDomain;
import com.sendpo.zjx.sendpo.fragment.helpingorder.BaseMyselfHelpOrderSingleFragment;
import com.sendpo.zjx.sendpo.fragment.helpingorder.MyselHelpOrder1Fragment;
import com.sendpo.zjx.sendpo.fragment.helpingorder.MyselHelpOrder2Fragment;
import com.sendpo.zjx.sendpo.net.HttpCode;
import com.sendpo.zjx.sendpo.net.HttpUrl;
import com.sendpo.zjx.sendpo.utils.Tools;
import com.sendpo.zjx.sendpo.utils.Xutils;
import com.shizhefei.view.indicator.IndicatorViewPager;
import com.shizhefei.view.indicator.ScrollIndicatorView;
import com.shizhefei.view.indicator.slidebar.ColorBar;
import com.shizhefei.view.indicator.transition.OnTransitionTextListener;
import com.shizhefei.view.viewpager.SViewPager;

import org.xutils.common.Callback;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/8/21.
 */

public class MyselfHelpingOrderSingleActivity extends AppCompatActivity {

    @BindView(R.id.backbt)
    RelativeLayout backbt;
    @BindView(R.id.indicator)
    ScrollIndicatorView indicator;
    @BindView(R.id.viewpager)
    SViewPager viewpager;
    @BindView(R.id.bottom)
    RelativeLayout bottom;
    @BindView(R.id.bottom_tv)
    Button bottomTv;
    private IndicatorViewPager indicatorViewPager;
    public static final String[] titles = new String[]{"进行中", "完成"};

    public int helpId = 0;
    public Dialog loadingdialog = null;
    public MyselHelpOrder1Fragment order1;
    public MyselHelpOrder2Fragment order2;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_myselfhelpingordersingle);
        ButterKnife.bind(this);
        Tools.initStatus(this, R.color.black);
        initComponent();
        initState();
    }

    public void initComponent() {
        helpId = getIntent().getIntExtra("helpId", 0);
        viewpager.setCanScroll(false);
        viewpager.setOffscreenPageLimit(1);
        indicatorViewPager = new IndicatorViewPager(indicator, viewpager);
        List<BaseMyselfHelpOrderSingleFragment> fragments = new ArrayList<>();
        fragments.add(order1=new MyselHelpOrder1Fragment());
        fragments.add(order2=new MyselHelpOrder2Fragment());
        indicatorViewPager.setAdapter(new FragmentIndicatorAdapter(getSupportFragmentManager(), fragments));
        indicatorViewPager.setClickIndicatorAnim(true);
        //点击切换颜色
        indicatorViewPager.setIndicatorOnTransitionListener(new OnTransitionTextListener().setColor(getResources().getColor(R.color.default_theme), getResources().getColor(R.color.default_gray)));
        //颜色块
        ColorBar colorBar = new ColorBar(this, getResources().getColor(R.color.default_theme), 5);
        colorBar.setWidth(120);
        indicatorViewPager.setIndicatorScrollBar(colorBar);
        indicatorViewPager.setOnIndicatorPageChangeListener(new IndicatorViewPager.OnIndicatorPageChangeListener() {
            @Override
            public void onIndicatorPageChange(int preItem, int currentItem) {
                switch (currentItem){
                    case 0:
                        if(order1!=null){
                            if(order1.pulltofresh!=null){
                                order1.pulltofresh.autoRefresh();
                            }
                        }
                        break;
                    case 1:
                        if(order2!=null){
                            if(order2.pulltofresh!=null){
                                order2.pulltofresh.autoRefresh();
                            }
                        }
                        break;

                }
            }
        });
        loadingdialog = new Dialog(this);
        loadingdialog.setContentView(R.layout.dialog_loading);
        loadingdialog.setCanceledOnTouchOutside(false);
        loadingdialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    }


    private class FragmentIndicatorAdapter extends IndicatorViewPager.IndicatorFragmentPagerAdapter {
        private List<BaseMyselfHelpOrderSingleFragment> fragments;

        public FragmentIndicatorAdapter(FragmentManager fragmentManager, List<BaseMyselfHelpOrderSingleFragment> fragments) {
            super(fragmentManager);
            this.fragments = fragments;
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        @Override
        public View getViewForTab(int position, View convertView, ViewGroup container) {
            if (convertView == null) {
                convertView = View.inflate(MyselfHelpingOrderSingleActivity.this, R.layout.tab_focus, null);
            }
            TextView textView = (TextView) convertView;
            textView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            textView.setText(titles[position]);
            return textView;
        }

        @Override
        public Fragment getFragmentForPage(int position) {
            return fragments.get(position);
        }
    }

    @OnClick(value = {R.id.backbt})
    public void onMyClick(View view) {
        switch (view.getId()) {
            case R.id.backbt:
                finish();
                break;

        }
    }

    public void initState() {
        Map<String, String> map = new HashMap<>();
        map.put("helpid", "" + helpId);
        Xutils.POST(HttpUrl.userneedhelpquerystate, map, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {

                Gson gson=new Gson();
                UserNeedHelpDomain domain= gson.fromJson(result,UserNeedHelpDomain.class);
                switch (domain.getState()) {
                    case HttpCode.SUCCESS:
                        if (domain.getData().getHelpState() == 0) {
                            bottomTv.setText("取消发布");
                            bottomTv.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            updateState(helpId+"",1+"");
                        }
                    });
                        } else if (domain.getData().getHelpState() == 1) {
                            bottomTv.setText("开始发布");
                            bottomTv.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    updateState(helpId+"",0+"");
                                }
                            });
                        }
                        break;
                    case HttpCode.ERROR:

                        break;
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }



    public void updateState(String  helpid,String helpState) {
        loadingdialog.show();

        Map<String, String> map = new HashMap<>();
        map.put("helpid", "" + helpid);
        map.put("helpState", "" + helpState);
        Xutils.POST(HttpUrl.userneedhelpupdatestate, map, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                loadingdialog.dismiss();

                Gson gson=new Gson();
                UserNeedHelpDomain domain= gson.fromJson(result,UserNeedHelpDomain.class);
                switch (domain.getState()) {
                    case HttpCode.SUCCESS:
                        if (domain.getData().getHelpState() == 0) {
                            Toast.makeText(getApplicationContext(),"取消发布",Toast.LENGTH_SHORT).show();
                            bottomTv.setText("取消发布");
                            bottomTv.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    updateState(helpId+"",1+"");
                                }
                            });
                        } else if (domain.getData().getHelpState() == 1) {
                            Toast.makeText(getApplicationContext(),"开始发布",Toast.LENGTH_SHORT).show();
                            bottomTv.setText("开始发布");
                            bottomTv.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    updateState(helpId+"",0+"");
                                }
                            });
                        }
                        break;
                    case HttpCode.ERROR:

                        break;
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                loadingdialog.dismiss();
                Toast.makeText(getApplicationContext(),"请稍后再试",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
}
